package com.tengju.bff.interfaces.manage.crm.brand;

import com.tengju.bff.interfaces.manage.crm.brand.facade.BrandMarketSortFacade;
import com.tengju.bff.interfaces.manage.crm.brand.form.BrandMarketSortKAQueryForm;
import com.tengju.bff.interfaces.manage.crm.brand.form.BrandMarketSortSECQueryForm;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.export.BrandMarketSecExportQuery;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.export.application.ExportService;
import com.tengju.export.domain.record.ExportDataType;
import com.tengju.export.domain.record.ExportFileType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Date: 2021/5/10 16:30
 * @Description:
 */
@Validated
@RestController
@Api(tags = "生意参谋-品牌分析-销售榜")
@RequestMapping(value = "manage/crm/brand/market")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class BrandMarketSortController {

    private final BrandMarketSortFacade brandMarketSortFacade;
    private final ExportService exportService;

    @ApiOperation(value = "品牌销售总榜（KA）", notes = "品牌销售总榜（KA）")
    @RequestMapping(value = "/ka/getKABrandMarketSortInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> getKABrandMarketSortInfo(BrandMarketSortKAQueryForm brandMarketSortKAQueryForm) {
        TableVO vo = brandMarketSortFacade.queryKABrandMarketSortInfo(brandMarketSortKAQueryForm);
        return ApiResponse.newSuccess(vo);
    }

    @ApiOperation(value = "品牌销售总榜（SEC）", notes = "品牌销售总榜（SEC）")
    @RequestMapping(value = "/sec/getSECBrandMarketSortInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> getSECBrandMarketSortInfo(BrandMarketSortSECQueryForm brandMarketSortSECQueryForm) {
        TableVO vo = brandMarketSortFacade.querySECBrandMarketSortInfo(brandMarketSortSECQueryForm);
        return ApiResponse.newSuccess(vo);
    }

    @ApiOperation(value = "导出SEC品牌分析品牌销售榜", notes = "导出SEC品牌分析品牌销售榜")
    @RequestMapping(value = "/sec/export", method = RequestMethod.GET)
    public ApiResponse<Object> asyncExport(BrandMarketSortSECQueryForm exportForm) {
        long staffId = ServletContextUtil.getServletContext().getStaffId().getValue();
        String realName = ServletContextUtil.getServletContext().getRealName();
        BrandMarketSecExportQuery exportQuery = new BrandMarketSecExportQuery();
        exportQuery.setPageIndex(1);
        exportQuery.setPageSize(50000);
        exportQuery.setDateType(exportForm.getDateType());
        exportQuery.setSecIdCode(exportForm.getSecIdCode());
        exportQuery.setMinSellAccount(exportForm.getMinSellAccount());
        exportQuery.setMaxSellAccount(exportForm.getMaxSellAccount());
        exportQuery.setOrderColumn(exportForm.getOrderColumn());
        exportQuery.setOrderType(exportForm.getOrderType());
        exportService.exportData("SEC品牌分析品牌销售榜列表", ExportFileType.XLSX,
                "brandMarketSecExportDataSourceRepository",
                ExportDataType.TABLE, exportQuery,
                staffId, realName);
        return ApiResponse.newSuccess();
    }
}
